package rursu.dal.managers;

import java.util.ArrayList;
import java.util.List;

import rursu.business.pojo.Movie;
import rursu.dal.managers.interfaces.IMovieDAL;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MovieDALManager implements IMovieDAL {	
	
	private SQLiteDatabase db;
	
	public static final String TABLE_NAME = "movies";
	
	protected static final String SCORE = "score";		
	protected static final String ADULT = "adult";			
	protected static final String ORIGINAL_NAME = "original_name";	
	protected static final String NAME = "name";	
	protected static final String ALTERNATIVE_NAME = "alternative_name";	
	protected static final String TYPE = "type";	
	protected static final String ID = "id";			
	protected static final String VOTES = "votes";	
	protected static final String RATING = "rating";		
	protected static final String CERTIFICATION = "certification";
	protected static final String OVERVIEW = "overview";
	protected static final String RELEASED = "released";
	protected static final String LAST_MODIFIED = "last_modified";
	
	public MovieDALManager(SQLiteDatabase db) {
		this.db = db;		
	}
	
	public long insert(Movie movie) {
		
		ContentValues initialValues = new ContentValues();
		
        initialValues.put(MovieDALManager.SCORE, movie.getScore());
        initialValues.put(MovieDALManager.ADULT, movie.isAdult());
        initialValues.put(MovieDALManager.ORIGINAL_NAME, movie.getOriginal_name());
        initialValues.put(MovieDALManager.NAME, movie.getName());
        initialValues.put(MovieDALManager.ALTERNATIVE_NAME, movie.getAlternative_name());
        initialValues.put(MovieDALManager.TYPE, movie.getType());
        initialValues.put(MovieDALManager.ID, movie.getId());
        initialValues.put(MovieDALManager.VOTES, movie.getVotes());
        initialValues.put(MovieDALManager.RATING, movie.getRating());
        initialValues.put(MovieDALManager.CERTIFICATION, movie.getCertification());
        initialValues.put(MovieDALManager.RELEASED, movie.getReleased());
        initialValues.put(MovieDALManager.OVERVIEW, movie.getOverview());
        initialValues.put(MovieDALManager.LAST_MODIFIED, movie.getLastModified());

        return db.insert(TABLE_NAME, null, initialValues);
	}

	public void delete(int id) {
		db.delete(TABLE_NAME, MovieDALManager.ID + "=" + id, null);
	}

	public Movie get(int id) {
		
		Cursor cursor = db.query(TABLE_NAME, new String [] {
														MovieDALManager.SCORE, 
														MovieDALManager.ADULT, 
														MovieDALManager.ORIGINAL_NAME, 
														MovieDALManager.NAME, 
														MovieDALManager.ALTERNATIVE_NAME, 
														MovieDALManager.TYPE, 
														MovieDALManager.VOTES,
														MovieDALManager.RATING,
														MovieDALManager.CERTIFICATION,
														MovieDALManager.OVERVIEW,
														MovieDALManager.LAST_MODIFIED,
														MovieDALManager.RELEASED },
														MovieDALManager.ID + "=" + id, null, null, null, null, "1");
		
		Movie movie = null;
		
		if ( cursor.getCount() > 0 ) {
			
			movie = new Movie();
			
			cursor.moveToFirst();
			
			movie.setScore( cursor.getInt( cursor.getColumnIndex(MovieDALManager.SCORE) ) );		
			movie.setAdult( Boolean.parseBoolean(cursor.getString( cursor.getColumnIndex(MovieDALManager.ADULT) )) );
			movie.setOriginal_name( cursor.getString( cursor.getColumnIndex(MovieDALManager.ORIGINAL_NAME) ) );
			movie.setName( cursor.getString( cursor.getColumnIndex(MovieDALManager.NAME) ) );
			movie.setAlternative_name( cursor.getString( cursor.getColumnIndex(MovieDALManager.ALTERNATIVE_NAME) ) );
			movie.setType( cursor.getString( cursor.getColumnIndex(MovieDALManager.TYPE) ) );
			movie.setVotes( cursor.getInt( cursor.getColumnIndex(MovieDALManager.VOTES) ) );
			movie.setRating( cursor.getFloat( cursor.getColumnIndex(MovieDALManager.RATING) ) );
			movie.setCertification( cursor.getString( cursor.getColumnIndex(MovieDALManager.CERTIFICATION) ) );
			movie.setReleased( cursor.getString( cursor.getColumnIndex(MovieDALManager.RELEASED) ) );
			movie.setOverview( cursor.getString( cursor.getColumnIndex(MovieDALManager.OVERVIEW) ) );
			movie.setLastModified( cursor.getString( cursor.getColumnIndex(MovieDALManager.LAST_MODIFIED) ) );
			movie.setId(id);			
			
		}
		
		cursor.close();
		
		return movie;
		
	}

	public int update(Movie movie) {
		
		int numOfUpdated = 0;
		
		ContentValues values = new ContentValues();
		
        values.put(MovieDALManager.SCORE, movie.getScore());
        values.put(MovieDALManager.ADULT, movie.isAdult());
        values.put(MovieDALManager.ORIGINAL_NAME, movie.getOriginal_name());
        values.put(MovieDALManager.NAME, movie.getName());
        values.put(MovieDALManager.ALTERNATIVE_NAME, movie.getAlternative_name());
        values.put(MovieDALManager.TYPE, movie.getType());
        values.put(MovieDALManager.ID, movie.getId());
        values.put(MovieDALManager.VOTES, movie.getVotes());
        values.put(MovieDALManager.RATING, movie.getRating());
        values.put(MovieDALManager.CERTIFICATION, movie.getCertification());
        values.put(MovieDALManager.RELEASED, movie.getReleased());
        values.put(MovieDALManager.OVERVIEW, movie.getOverview());
        values.put(MovieDALManager.LAST_MODIFIED, movie.getLastModified());
        
		if ( get(movie.getId()) != null ) {
			numOfUpdated = db.update(TABLE_NAME, values, MovieDALManager.ID + "=" + movie.getId(), null);
		} else {
			db.insert(TABLE_NAME, null, values);
		}
		
		return numOfUpdated;
	}

	public List<Movie> getAll() {
		Cursor cursor = db.query(false, TABLE_NAME, new String [] {
				MovieDALManager.SCORE, 
				MovieDALManager.ADULT, 
				MovieDALManager.ORIGINAL_NAME, 
				MovieDALManager.NAME, 
				MovieDALManager.ALTERNATIVE_NAME, 
				MovieDALManager.TYPE, 
				MovieDALManager.VOTES,
				MovieDALManager.RATING,
				MovieDALManager.CERTIFICATION,
				MovieDALManager.RELEASED,
				MovieDALManager.OVERVIEW,
				MovieDALManager.LAST_MODIFIED,
				MovieDALManager.ID 				
				},
				null, null, null, null, null, null);

		List<Movie> movies = new ArrayList<Movie>();
		
		cursor.moveToFirst();
		
		while ( !cursor.isAfterLast() ) {
			
			Movie movie = new Movie();
			
			movie.setScore( cursor.getInt( cursor.getColumnIndex(MovieDALManager.SCORE) ) );		
			movie.setAdult( Boolean.parseBoolean(cursor.getString( cursor.getColumnIndex(MovieDALManager.ADULT) )) );
			movie.setOriginal_name( cursor.getString( cursor.getColumnIndex(MovieDALManager.ORIGINAL_NAME) ) );
			movie.setName( cursor.getString( cursor.getColumnIndex(MovieDALManager.NAME) ) );
			movie.setAlternative_name( cursor.getString( cursor.getColumnIndex(MovieDALManager.ALTERNATIVE_NAME) ) );
			movie.setType( cursor.getString( cursor.getColumnIndex(MovieDALManager.TYPE) ) );
			movie.setVotes( cursor.getInt( cursor.getColumnIndex(MovieDALManager.VOTES) ) );
			movie.setRating( cursor.getFloat( cursor.getColumnIndex(MovieDALManager.RATING) ) );
			movie.setCertification( cursor.getString( cursor.getColumnIndex(MovieDALManager.CERTIFICATION) ) );
			movie.setReleased( cursor.getString( cursor.getColumnIndex(MovieDALManager.RELEASED) ) );
			movie.setOverview( cursor.getString( cursor.getColumnIndex(MovieDALManager.OVERVIEW) ) );
			movie.setLastModified( cursor.getString( cursor.getColumnIndex(MovieDALManager.LAST_MODIFIED) ) );
			movie.setId( cursor.getInt( cursor.getColumnIndex(MovieDALManager.ID) ) );
			
			movies.add(movie);
			
			cursor.moveToNext();
		}		
		
		cursor.close();
		
		return movies;
	}

	public List<Movie> getMoviesByListId(int listId) {
		Cursor cursor = db.rawQuery("SELECT " 	+ MovieDALManager.SCORE + ","
												+ MovieDALManager.ADULT + ","
												+ MovieDALManager.ORIGINAL_NAME + ","
												+ MovieDALManager.NAME + ","
												+ MovieDALManager.ALTERNATIVE_NAME + "," 
												+ MovieDALManager.TYPE + ","
												+ MovieDALManager.VOTES + ","
												+ MovieDALManager.RATING + ","
												+ MovieDALManager.CERTIFICATION + ","
												+ MovieDALManager.LAST_MODIFIED + ","
												+ MovieDALManager.RELEASED + ","
												+ MovieDALManager.ID + " FROM "
												+ MovieDALManager.TABLE_NAME + " INNER JOIN " + MoviesListPairDALManager.TABLE_NAME + " ON " 
												+ MoviesListPairDALManager.TABLE_NAME + "." + MoviesListPairDALManager.MOVIE_ID + " = "
												+ MovieDALManager.TABLE_NAME + "." + MovieDALManager.ID + " WHERE "
												+ MoviesListPairDALManager.LIST_ID + " = " + listId, null);
		
		cursor.moveToFirst();
		
		List<Movie> movies = new ArrayList<Movie>();
		
		while ( !cursor.isAfterLast() ) {
			
			Movie movie = new Movie();
			
			movie.setScore( cursor.getInt( cursor.getColumnIndex(MovieDALManager.SCORE) ) );		
			movie.setAdult( Boolean.parseBoolean(cursor.getString( cursor.getColumnIndex(MovieDALManager.ADULT) )) );
			movie.setOriginal_name( cursor.getString( cursor.getColumnIndex(MovieDALManager.ORIGINAL_NAME) ) );
			movie.setName( cursor.getString( cursor.getColumnIndex(MovieDALManager.NAME) ) );
			movie.setAlternative_name( cursor.getString( cursor.getColumnIndex(MovieDALManager.ALTERNATIVE_NAME) ) );
			movie.setType( cursor.getString( cursor.getColumnIndex(MovieDALManager.TYPE) ) );
			movie.setVotes( cursor.getInt( cursor.getColumnIndex(MovieDALManager.VOTES) ) );
			movie.setRating( cursor.getFloat( cursor.getColumnIndex(MovieDALManager.RATING) ) );
			movie.setCertification( cursor.getString( cursor.getColumnIndex(MovieDALManager.CERTIFICATION) ) );
			movie.setReleased( cursor.getString( cursor.getColumnIndex(MovieDALManager.RELEASED) ) );
			movie.setLastModified( cursor.getString( cursor.getColumnIndex(MovieDALManager.LAST_MODIFIED) ) );
			movie.setId( cursor.getInt( cursor.getColumnIndex(MovieDALManager.ID) ) );
			
			movies.add(movie);
			
			cursor.moveToNext();
		}		
		
		cursor.close();
		
		return movies;
	}	
	
}
